package com.qingpeng.pay.service;

import android.app.Notification;
import android.os.Bundle;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public abstract class NotificationHandle {
    protected String pkgtype;
    protected Notification notification;
    protected Bundle extras;
    protected String title;
    protected String content;
    protected String notitime;
    protected IDoPost postpush;

    public NotificationHandle(String pkgtype, Notification notification, IDoPost postpush) {
        this.pkgtype = pkgtype;
        this.notification = notification;
        this.postpush = postpush;

        this.extras = notification.extras;
        // 获取通知标题
        title = extras.getString(Notification.EXTRA_TITLE, "");
        // 获取通知内容
        content = extras.getString(Notification.EXTRA_TEXT, "");
        long when = notification.when;
        Date date = new Date(when);
//                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                notitime=format.format(date);

        int secondTimestamp = getSecondTimestamp(date);
        notitime = secondTimestamp+"";


    }
    /**
     * 获取精确到秒的时间戳
     * @return
     */
    public static int getSecondTimestamp(Date date){
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime());
        int length = timestamp.length();
        if (length > 3) {
            return Integer.valueOf(timestamp.substring(0,length-3));
        } else {
            return 0;
        }
    }

    public abstract void handleNotification();

    protected String extractMoney(String content) {
        Pattern pattern = Pattern.compile("(收款|向你付款|到账)(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?元");
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            String tmp = matcher.group();
            Pattern patternnum = Pattern.compile("(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?");
            Matcher matchernum = patternnum.matcher(tmp);
            if (matchernum.find())
                return matchernum.group();
            return null;
        } else
            return null;


    }


}

